package com.gitee.controller.user;

import com.gitee.dto.AddMemberDTO;
import com.gitee.dto.GroupDTO;
import com.gitee.dto.SaveGroupChatDTO;
import com.gitee.entity.Group;
import com.gitee.result.Result;
import com.gitee.service.GroupService;
import com.gitee.vo.GroupChatVO;
import com.gitee.vo.GroupVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
@Api(tags = "群聊接口")
@RequestMapping("/user/groupController")
public class GroupController {

    @Resource
    private GroupService groupService;

    @PostMapping("/createGroup")
    @ApiOperation("新建群聊")
    public Result<?> createGroup(GroupDTO groupDTO) {
        log.info("新建群聊: {}", groupDTO);
        groupService.createGroup(groupDTO);
        return Result.success();
    }

    @PostMapping("/addMember")
    @ApiOperation("添加成员")
    public Result<?> addMember(AddMemberDTO addMemberDTO) {
        log.info("添加成员: {}", addMemberDTO);
        groupService.addMember(addMemberDTO);
        return Result.success();
    }

    @DeleteMapping("/removeMember")
    @ApiOperation("踢出成员")
    public Result<?> removeMember(AddMemberDTO addMemberDTO) {
        log.info("踢出成员: {}", addMemberDTO);
        groupService.removeMember(addMemberDTO);
        return Result.success();
    }

    @DeleteMapping("/deleteGroup/{groupId}")
    @ApiOperation("解散群聊")
    public Result<?> deleteGroup(@PathVariable Integer groupId) {
        log.info("解散群聊: {}", groupId);
        groupService.deleteGroup(groupId);
        return Result.success();
    }

    @PutMapping("/editGroup")
    @ApiOperation("编辑群聊信息")
    public Result<?> editGroup(@RequestBody Group group) {
        log.info("编辑群聊信息: {}", group);
        groupService.editGroup(group);
        return Result.success();
    }

    @DeleteMapping("/leaveGroup/{groupId}")
    @ApiOperation("退出群聊")
    @CacheEvict(cacheNames = "groupListCache", allEntries = true)
    public Result<?> leaveGroup(@PathVariable Integer groupId) {
        log.info("退出群聊: {}", groupId);
        groupService.leaveGroup(groupId);
        return Result.success();
    }

    @GetMapping("/getGroupInfo/{groupId}")
    @ApiOperation("查询群成员信息")
    public Result<GroupVO> getGroupInfo(@PathVariable Integer groupId) {
        log.info("查询群成员信息: {}", groupId);
        return Result.success(groupService.getGroupInfo(groupId));
    }

    @PostMapping("/saveGroupChat")
    @ApiOperation("发送群聊信息")
    public Result<?> saveGroupChat(SaveGroupChatDTO saveGroupChatDTO) {
        log.info("发送群聊信息: {}", saveGroupChatDTO);
        groupService.saveGroupChat(saveGroupChatDTO);
        return Result.success();
    }

    @GetMapping("/getGroupChat/{groupId}")
    @ApiOperation("查看聊天记录")
    public Result<List<GroupChatVO>> getGroupChat(@PathVariable Integer groupId) {
        log.info("查看群聊信息: {}", groupId);
        return Result.success(groupService.getGroupChat(groupId));
    }

    @GetMapping("/getUserJoinGroup/{userId}")
    @ApiOperation("查看某用户加入的群聊")
    public Result<List<Group>> getUserJoinGroup(@PathVariable Integer userId) {
        log.info("查看某用户加入的群聊: {}", userId);
        return Result.success(groupService.getUserJoinGroup(userId));
    }
}

